//Component Object
const app = getApp();


Component({
  properties: {
    title:{
      type: String,
      value: '',

    },
    initData: {
      type: Array,
      value: [],
      observer: function (e) {
        if (e) {
          this.setData({
            data: e,
           
          })
        }
      }
    },

    isEdit: {
      type: Boolean,
      value: true,
      observer: function (e) {
        if (e) {
          this.setData({
            isEdit: e,
          })
        }
      }
    },


    headers:{
      type: Array,
      value: [],
      observer: function (e) {
        if (e) {
          const init = [{}];

          e.forEach(item => {
            init[0][item.value]=""
          });
          this.setData({
            data: e,
            init:init
          })
        }
      }
    }
    

  },
  data: {
    data: [],
    init:[],
    isEdit:true,
  },
  methods: {
    add(){

      console.log('this.data.data',this.data.data);

      this.setData({
        
        data:[...this.data.data,{...this.data.initData}]
      })

    },
    del(e){
      if(this.data.data.length==1){
        console.log('da',this.data.init)
        this.setData({
          
          data:this.data.init
        })
      }
      else{
        const index=e.currentTarget.dataset.index;
        this.data.data.splice(index,1);
        this.setData({
          data:this.data.data
        })
      }
    },
    inputChange(e){
       const index =  e.target.dataset.index;
      const headerItem = e.target.dataset.headeritem;
      const newData = JSON.parse(JSON.stringify(this.data.data));
      newData[index][headerItem.value] = e.detail.detail.value;

      this.setData({
        data:newData

      })

    }
  },
  lifetimes: {
    created: function () {

    },
    attached: function () {

    },
    ready: function () {

    },
    moved: function () {

    },
    detached: function () {

    }
  }

});